package com.zking.ssm.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.zking.ssm.sys.model.HotelMember;
import com.zking.ssm.sys.model.HotelSellerEmployee;
import com.zking.ssm.sys.service.IHotelMemberService;
import com.zking.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/HotelMember")
public class HotelMemderController {

    @Autowired
    private IHotelMemberService Service;

    @ResponseBody
    @RequestMapping("/list")
    public Map<String,Object> list (HotelMember hotelMember, PageBean pageBean,HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        pageBean.setRequest(request);
        pageBean.setPage(pageBean.getPage());
        pageBean.setRows(pageBean.getRows());
        pageBean.setTotal(pageBean.getTotal());
        List<HotelMember> userList = Service.list(hotelMember, pageBean);
        map.put("code", 0);
        map.put("result", userList);
        map.put("page", pageBean.getPage());
        map.put("rows", pageBean.getRows());
        map.put("total", pageBean.getTotal());
        return map;
    }


    @RequestMapping("/add")
    public Map<String,Object> Add (HotelMember hotelMember){
        Map<String,Object> map = new HashMap<>();
        hotelMember.setJoinTime(new Date());
        int insert = Service.insert(hotelMember);
        if (insert>0){
            map.put("code",0);
            map.put("message","增加成功");
        }else {
            map.put("code",-1);
            map.put("message","增加失败");
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("/get")
    public HotelMember get(HotelMember hotelMember){
        HotelMember hotelMember1 = Service.selectByPrimaryKey(hotelMember.getId());
//        request.setAttribute("message","查询成功");
        return hotelMember1;
    }

    @RequestMapping("/del")
    public Map<String,Object> del (HotelMember hotelMember){
        Map<String,Object> map = new HashMap<>();
        int insert = Service.deleteByPrimaryKey(hotelMember.getId());
        if (insert>0){
            map.put("code",0);
            map.put("message","删除成功");
        }else {
            map.put("code",-1);
            map.put("message","删除失败");
        }
        return map;
    }
    @RequestMapping("/de")
    public Map<String,Object> de (String id){
        Map<String,Object> map = new HashMap<>();
        //把前端传过来的json字符串变成数组
        HotelMember[] sysUserIds = JSONObject.parseObject(id, HotelMember[].class);
        if (id != null) {
            for (HotelMember ids : sysUserIds) {
                Service.deleteByPrimaryKey(ids.getId());
                map.put("message", "删除成功");
            }
        } else {
            map.put("message", "删除失败");
        }
        return map;
    }
    @RequestMapping("/up")
    public Map<String,Object> up (HotelMember hotelMember){
        Map<String,Object> map = new HashMap<>();
        int insert = Service.updateByPrimaryKey(hotelMember);
        if (insert>0){
            map.put("code",0);
            map.put("message","修改成功");
        }else {
            map.put("code",-1);
            map.put("message","修改失败");
        }
        return map;
    }

}
